package com.example.powermock.controller;

import com.example.powermock.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired(required = false)
    HelloService helloService;

    @GetMapping("/test")
    public String testHello () {
        String hello = null;
        try {
            Thread.sleep(1000);
            hello = helloService.sayHi("hello world");
            System.out.println("hello param is : " + hello);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hello != null ? hello : "error";
    }

    @GetMapping("/testSome")
    public String doHttpGet(String uri, Map<String, String> getParams) {
        if (StringUtils.isEmpty(uri)) {
            return "uri is null";
        }
        String object = getParams.get("object");
        if (StringUtils.isEmpty(object)) {
            return "object is null";
        } else {
            if (object.equalsIgnoreCase("ok")) {
                return "ok";
            } else {
                return "not ok";
            }
        }
    }
}
